_realname=onefetch
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=2.24.0
pkgrel=1
pkgdesc="Git repository summary on your terminal (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://github.com/o2sh/onefetch"
msys2_references=(
  'archlinux: onefetch'
  'purl: pkg:cargo/onefetch'
)
license=('spdx:MIT')
depends=("${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("${MINGW_PACKAGE_PREFIX}-rust"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
optdepends=("${MINGW_PACKAGE_PREFIX}-ttf-font-nerd: for glyphs in languages info")
source=("${url}/archive/${pkgver}/${_realname}-${pkgver}.tar.gz"
        "zstd-sys.tar.gz::https://crates.io/api/v1/crates/zstd-sys/2.0.13+zstd.1.5.6/download"
        "link-zstd-dynamically.patch")
sha256sums=('41f457c9a8145de94980bcae497d84a56cd75c1598a6a9eeb45984947bf4f1f8'
            '38ff0f21cfee8f97d94cef41359e0c89aa6113028ab0291aa8ca0038995a95aa'
            '63ecb84980c39516bc412a3cc47a0965c4349af77c2ab3d84ea02892ec0fad71')

_env() {
  export ZSTD_SYS_USE_PKG_CONFIG=1
  export WINAPI_NO_BUNDLED_LIBRARIES=1
}

prepare() {
  cd "${_realname}-${pkgver}"

  patch -d "${srcdir}/zstd-sys-2.0.13+zstd.1.5.6" -i "${srcdir}/link-zstd-dynamically.patch"
  cat >> Cargo.toml <<END

[patch.crates-io]
zstd-sys = { path = "../zstd-sys-2.0.13+zstd.1.5.6" }
END

  cargo update -p zstd-sys
  cargo fetch --locked --target "$(rustc -vV | sed -n 's/host: //p')"
}

build() {
  cd "${_realname}-${pkgver}"

  _env
  cargo build --release --frozen
}

check() {
  cd "${_realname}-${pkgver}"

  _env
  cargo test --release --frozen
}

package() {
  cd "${_realname}-${pkgver}"

  install -Dm755 "target/release/${_realname}" "${pkgdir}${MINGW_PREFIX}/bin/${_realname}.exe"

  install -Dm644 LICENSE.md -t "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}"
  install -Dm644 "docs/${_realname}.1" "${pkgdir}${MINGW_PREFIX}/share/man/man1/${_realname}.1"

  local _complete="target/release/onefetch --generate"
  $_complete bash | install -Dm664 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/bash-completion/completions/${_realname}"
  $_complete fish | install -Dm664 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/fish/vendor_completions.d/${_realname}.fish"
  $_complete zsh | install -Dm664 /dev/stdin "${pkgdir}${MINGW_PREFIX}/share/zsh/site-functions/_${_realname}"
}
